package com.amsp.oa.oademo.controller;

import com.amsp.oa.oademo.common.Result;
import com.amsp.oa.oademo.entity.WenjianXiuding;
import com.amsp.oa.oademo.service.WenjianXiudingService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

@Slf4j
@RestController
@RequestMapping("/api/wenjian-xiuding")
@Api(tags = "文件修订申请管理")
public class WenjianXiudingController {

    @Autowired
    private WenjianXiudingService wenjianXiudingService;

    @ApiOperation("分页查询文件修订申请")
    @GetMapping("/page")
    public Result<IPage<WenjianXiuding>> getPage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String fileName,
            @RequestParam(required = false) String fileNumber,
            @RequestParam(required = false) String applyDepartment) {
        return Result.ok(wenjianXiudingService.getPage(current, size, fileName, fileNumber, applyDepartment));
    }

    @ApiOperation("获取文件修订申请详情")
    @GetMapping("/{id}")
    public Result<WenjianXiuding> getDetail(@PathVariable Long id) {
        return Result.ok(wenjianXiudingService.getDetailById(id));
    }

    @ApiOperation("保存或更新文件修订申请")
    @PostMapping("/save")
    public Result<Boolean> saveOrUpdate(@RequestBody WenjianXiuding wenjianXiuding) {
        log.info("接收到的保存数据: {}", wenjianXiuding);
        if (wenjianXiuding.getApplyDate() == null) {
            wenjianXiuding.setApplyDate(LocalDate.now());
        }
        boolean result = wenjianXiudingService.saveWithDetails(wenjianXiuding);
        log.info("保存结果: {}", result);
        return Result.ok(result);
    }

    @ApiOperation("删除文件修订申请")
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        return Result.ok(wenjianXiudingService.removeById(id));
    }
} 